package com.example.supernote.data.network

import com.example.supernote.data.network.AudioApi.UploadAudioApi.UploadAudioApiService
import com.example.supernote.data.network.config.CreateAudioResponse
import com.example.supernote.data.network.config.ImageOCRResponse
import com.example.supernote.data.network.config.ProgressAudioResponse
import com.example.supernote.data.network.config.RequestBody
import com.example.supernote.data.network.config.Response
import com.example.supernote.data.network.config.ResultAudioResponse
import com.example.supernote.data.network.config.RunAudioBody
import com.example.supernote.data.network.config.RunAudioResponse
import com.example.supernote.data.network.config.TextGeneration
import com.example.supernote.data.network.config.TranslateTextResponse
import com.example.supernote.data.network.config.UploadAudioResponse
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.FieldMap
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.HeaderMap
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Query
import retrofit2.http.QueryMap

private const val BASE_URL = "https://api-ai.vivo.com.cn"

private val json = Json {
    ignoreUnknownKeys = true
    coerceInputValues = true
}

private val retrofit = Retrofit.Builder()
    .addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
    .addConverterFactory(json.asConverterFactory("application/x-www-form-urlencoded".toMediaType()))
    .baseUrl(BASE_URL)
    .build()

// 文本生成api
object TextGenerationApi {
    interface TextGenerationApiService {
        @POST("/vivogpt/completions")
        suspend fun generateText(
            @HeaderMap headers: Map<String, String>,
            @Query("requestId") requestId: String,
            @Body textPrompt: Map<String, String>
        ): TextGeneration
    }
    const val GENERATE_TEXT_URI = "/vivogpt/completions"
    val retrofitService: TextGenerationApiService by lazy {
        retrofit.create(TextGenerationApiService::class.java)
    }
}

// 图片api
object ImageApi {
    // 图片生成api
    object ImageGenerationApi {
        interface ImageGenerationApiService {
            @POST("/api/v1/task_submit")
            suspend fun generateImage(
                @HeaderMap headers: Map<String, String>,
                @Body requestBody: RequestBody
            ): Response
        }
        const val GENERATE_IMAGE_URI = "/api/v1/task_submit"
        val retrofitService: ImageGenerationApiService by lazy {
            retrofit.create(ImageGenerationApiService::class.java)
        }
    }

    // 图片查询api
    object ImageQueryApi {
        interface ImageQueryApiService {
            @GET("/api/v1/task_progress")
            suspend fun queryImage(
                @HeaderMap headers: Map<String, String>,
                @Query("task_id") task_id: String,
            ): Response
        }
        const val QUERY_IMAGE_URI = "/api/v1/task_progress"
        val retrofitService: ImageQueryApiService by lazy {
            retrofit.create(ImageQueryApiService::class.java)
        }
    }
}

// 图像OCR
object ImageOCRApi {
    interface ImageOCRApiService {
        @FormUrlEncoded
        @POST("/ocr/general_recognition")
        suspend fun getOCRResult (
            @HeaderMap headers: Map<String, String>,
            @FieldMap requestBody: Map<String, String>
        ): ImageOCRResponse
    }
    const val IMAGE_OCR_URI = "/ocr/general_recognition"
    val retrofitService: ImageOCRApiService by lazy {
        retrofit.create(ImageOCRApiService::class.java)
    }
}

// 文本翻译api
object TranslateTextApi {
    interface TranslateTextApiService {
        @FormUrlEncoded
        @POST("/translation/query/self")
        suspend fun getTranslateResult (
            @HeaderMap headers: Map<String, String>,
            @FieldMap requestBody: Map<String, String>
        ): TranslateTextResponse
    }
    const val TRANSLATE_TEXT_URI = "/translation/query/self"
    val retrofitService: TranslateTextApiService by lazy {
        retrofit.create(TranslateTextApiService::class.java)
    }
}

// 音频api
object AudioApi {
    // 创建音频api
    object CreateAudioApi {
        interface CreateAudioApiService {
            @POST("/lasr/create")
            suspend fun getCreateAudioResult (
                @HeaderMap headers: Map<String, String>,
                @QueryMap queryMap: Map<String, String>,
                @Body createAudioBody: Map<String, String>
            ): CreateAudioResponse
        }
        const val CREATE_AUDIO_URI = "/lasr/create"
        val retrofitService: CreateAudioApiService by lazy {
            retrofit.create(CreateAudioApiService::class.java)
        }
    }
    // 上传音频api
    object UploadAudioApi {
        interface UploadAudioApiService {
            @Multipart
            @POST("/lasr/upload")
            suspend fun getUploadAudioResult (
                @HeaderMap headers: Map<String, String>,
                @QueryMap queryMap: Map<String, String>,
                @Part file: MultipartBody.Part
            ): UploadAudioResponse
        }
        const val UPLOAD_AUDIO_URI = "/lasr/upload"
        val retrofitService: UploadAudioApiService by lazy {
            retrofit.create(UploadAudioApiService::class.java)
        }
    }
    // 创建转写任务并运行api
    object RunAudioApi {
        interface RunAudioApiService {
            @POST("/lasr/run")
            suspend fun getRunAudioResult (
                @HeaderMap headers: Map<String, String>,
                @QueryMap queryMap: Map<String, String>,
                @Body runAudioBody: Map<String, String>
            ): RunAudioResponse
        }
        const val RUN_AUDIO_URI = "/lasr/run"
        val retrofitService: RunAudioApiService by lazy {
            retrofit.create(RunAudioApiService::class.java)
        }
    }
    // 查询进度api
    object ProgressAudioApi {
        interface ProgressAudioApiService {
            @POST("/lasr/progress")
            suspend fun getProgressAudioResult (
                @HeaderMap headers: Map<String, String>,
                @QueryMap queryMap: Map<String, String>,
                @Body progressAudioBody: Map<String, String>
            ): ProgressAudioResponse
        }
        const val PROGRESS_AUDIO_URI = "/lasr/progress"
        val retrofitService: ProgressAudioApiService by lazy {
            retrofit.create(ProgressAudioApiService::class.java)
        }
    }
    // 查询结果api
    object ResultAudioApi {
        interface ResultAudioApiService {
            @POST("/lasr/result")
            suspend fun getResultAudioResult (
                @HeaderMap headers: Map<String, String>,
                @QueryMap queryMap: Map<String, String>,
                @Body resultAudioBody: Map<String, String>
            ): ResultAudioResponse
        }
        const val RESULT_AUDIO_URI = "/lasr/result"
        val retrofitService: ResultAudioApiService by lazy {
            retrofit.create(ResultAudioApiService::class.java)
        }
    }
}

